package ms.maplestory.boot.client.character.Inventory;

import ms.maplestory.boot.connection.db.FileTimeConverter;
import ms.maplestory.boot.enums.InventoryType;
import ms.maplestory.boot.enums.ItemType;
import ms.maplestory.boot.util.FileTime;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.io.Serializable;

@Getter
@Setter
@Entity
@Table(name = "items")
@Inheritance(strategy = InheritanceType.JOINED)
public class Item implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private long id;

    @Column(name = "inventoryId")
    private int inventoryId;
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "inventoryType")
    protected InventoryType inventoryType;

    protected int itemId;
    protected int position;
    protected int quantity;
    protected boolean isCash;

    @Enumerated(EnumType.ORDINAL)
    @Column(name = "type")
    protected ItemType type;

    @Convert(converter = FileTimeConverter.class)
    protected FileTime dateExpire = FileTime.fromType(FileTime.Type.MAX_TIME);

    private String owner = "";

    public Item() {
    }

    public Item deepCopy() {
        Item item = new Item();
        item.setItemId(getItemId());
        item.setPosition(getPosition());
        item.setDateExpire(getDateExpire().deepCopy());
        item.setInventoryType(getInventoryType());
        item.setCash(isCash());
        item.setType(getType());
        item.setOwner(getOwner());
        item.setQuantity(getQuantity());
        return item;
    }

    public void addQuantity(int amount) {
        if (amount > 0 && amount + quantity > 0) {
            quantity += amount;
        }
    }

    @Override
    public String toString() {
        return getClass().getName() + "{" +
                "(" + super.toString() +
                ")id=" + id +
                ", itemId=" + itemId +
                ", position=" + position +
                ", position=" + position +
                ", quantity=" + quantity +
                ", type=" + type +
                '}';
    }
}
